package com.woniuxy.bases.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.bases.entity.Material;
import com.woniuxy.bases.entity.dto.MaterialAddDTO;
import com.woniuxy.bases.entity.dto.MaterialGetDTO;
import com.woniuxy.bases.entity.dto.MaterialUpdateDTO;
import com.woniuxy.bases.mapper.MaterialMapper;
import com.woniuxy.bases.service.MaterialService;
import com.woniuxy.comments.util.CustomPage;
import com.woniuxy.comments.util.ResponseResult;
import com.woniuxy.comments.util.ResponseState;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MaterialServiceImpl implements MaterialService {
    @Resource
    private MaterialMapper materialMapper;
    @Override
    public ResponseResult<CustomPage<List<Material>>> getAllMaterial(MaterialGetDTO materialGetDTO) {
        PageHelper.startPage(materialGetDTO.getPage().getPageIndex(),materialGetDTO.getPage().getPageSize());
        List<Material> list=materialMapper.getAllMaterial(materialGetDTO.getMaterialStatus());
        PageInfo<Material> pageInfo=new PageInfo<>(list);
        CustomPage<List<Material>> customPage=new CustomPage<>();
        customPage.setTotal(pageInfo.getTotal());
        customPage.setSize(pageInfo.getSize());
        customPage.setData(list);
        customPage.setTotalPage(pageInfo.getPages());
        customPage.setCurrentPage(pageInfo.getPageNum());
        return new ResponseResult<CustomPage<List<Material>>>()
                .setMessage("success")
                .setState(ResponseState.SELECT_SUCCESS)
                .setData(customPage)
                .setCode(200);
    }

    @Override
    public boolean addNewMaterial(MaterialAddDTO materialAddDTO) {
        Material material=materialMapper.getLatestMaterial();
        materialAddDTO.setMaterialCode("CL"+material.getMaterialId()+1);
        return materialMapper.addNewMaterial(materialAddDTO);
    }

    @Override
    public boolean delMaterial(Integer materialId) {
        return materialMapper.delMaterial(materialId);
    }

    @Override
    public boolean updateMaterial(MaterialUpdateDTO materialUpdateDTO) {
        return materialMapper.updateMaterial(materialUpdateDTO);
    }
}
